package pers.li.controller;

import pers.li.model.Partner;
import pers.li.service.CategoryService;
import pers.li.service.GoodsService;
import pers.li.model.Category;
import pers.li.model.Goods;
import pers.li.service.PartnerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/index.action")
public class HomeController extends HttpServlet {
    private final GoodsService goodsService = new GoodsService();
    private final CategoryService categoryService = new CategoryService();
    private final PartnerService partnerService = new PartnerService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Goods> today = goodsService.getToday();
        List<Map<String, Object>> categoryAndGoods = categoryService.getCategoryAndGoods();
        List<Goods> hotGoods = goodsService.getHotGoods(6);
        List<List<Category>> category = categoryService.getCategory();
        HttpSession session = req.getSession();
        session.setAttribute("category", category);
        req.setAttribute("todays", today);
        req.setAttribute("categoryAndGoods", categoryAndGoods);
        req.setAttribute("hotGoods", hotGoods);
        List<Partner> partners = partnerService.getPartners();
        System.out.println(partners);
        session.setAttribute("partners", partners);
        req.getRequestDispatcher("/home.jsp").forward(req, resp);
    }
}
